package Utility;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

import Messages.Message;

public class PaxosMessageReaderImpl implements PaxosMessageReader {
	/**
	 *  Reads the message from the given client socket.
	 */
	@Override
	public Message readMessage(Socket clientSocket) {
		Message readMessageFromSocket = null;
		ObjectInputStream ois = null;
		System.out.println("Trying to read message from socket.... " );
		try {
			// Initialize object input and output stream
			InputStream serverSockInput = clientSocket.getInputStream();
			ois = new ObjectInputStream(serverSockInput);
			readMessageFromSocket = (Message) ois.readObject();
			if (readMessageFromSocket != null)
				System.out.println("Reading Message :" + readMessageFromSocket.getMessageType());
			//ois.close();
		} catch (Exception e) {
			System.err.println("Failed to read message. ");
			e.printStackTrace();
		}
		return readMessageFromSocket;
	}

}
